<?php


namespace app\index\controller\xilujob;


use app\common\controller\xilujob\Xilujob;
use app\common\model\xilujob\Config;
use app\common\model\xilujob\PackageOrder;
use app\common\model\xilujob\UserCollection;
use think\Db;
use think\Exception;

class Package extends Xilujob
{
    protected $noNeedLogin = [];

    protected $noNeedRight = ['*'];

    protected $restrictedActions = [];

    public function _initialize()
    {
        parent::_initialize(); // TODO: Change the autogenerated stub
        $this->model = new \app\common\model\xilujob\Package();
    }

    public function index(){
        $this->relationSearch = true;
        $this->request->filter(['strip_tags']);
        if ($this->request->isAjax()) {
            list($where, $sort, $order, $offset, $limit) = $this->buildparams();
            $lists = $this->model
                ->where($where)
                ->paginate($limit);
            $result = array("total" => $lists->total(), "rows" => $lists->items());

            return json($result);
        }
        $this->assign('title', '招聘套餐');
        return $this->view->fetch();
    }

    public function order($ids=NULL){
        $row = $this->model->get($ids);
        if(!$row){
            $this->error('非法参数');
        }
        if($this->request->isPost()){
            $params = $this->request->post('row/a');
            if(!$params){
                $this->error("参数错误");
            }
            $params['platform'] = 'pc';
            $params['package_id'] = $row->id;
            Db::startTrans();
            try {
               $ret = PackageOrder::createOrder($params,$this->company->id,$this->auth->id);
            }catch (Exception $e){
                Db::rollback();
                $this->error($e->getMessage());
            }
            Db::commit();
            $this->success('订单创建成功','',$ret);
        }
        $this->view->assign('row',$row);
        $this->view->assign('ticketTypeList',(new PackageOrder)->getTicketTypeList());
        $this->view->assign('corporate_company',Config::getdatas('corporate_company'));
        $this->view->assign('open_bank',Config::getdatas('open_bank'));
        $this->view->assign('corporate_account',Config::getdatas('corporate_account'));
        $this->assign('title', '订单确认');
        return $this->view->fetch();
    }

}